package org.mspring.mlog.web.module.common;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.mspring.mlog.entity.Catalog;
import org.mspring.mlog.entity.Tag;
import org.mspring.mlog.service.CatalogService;
import org.mspring.mlog.service.TagService;
import org.mspring.mlog.web.support.resolver.PathParam;
import org.mspring.platform.security.entity.Role;
import org.mspring.platform.security.entity.User;
import org.mspring.platform.security.service.RoleService;
import org.mspring.platform.security.service.UserService;
import org.mspring.platform.utils.StringUtils;
import org.mspring.platform.web.freemarker.widget.stereotype.Action;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Gao Youbo
 * @since 2013-1-15
 */
@Action
@RequestMapping("/common/validate")
public class ValidateMethodWidget {

    @Autowired
    private UserService userService;
    @Autowired
    private CatalogService catalogService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private TagService tagService;

    /**
     * 用户名是否存在
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/userNameExists")
    @ResponseBody
    public String userNameExists(@PathParam(required = false) String name, @PathParam(required = false) Long id, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isBlank(name)) {
            return "true";
        }
        try {
            User user = userService.findByUsername(name);
            if (user == null) {
                return "false";
            }
            if (id != null && id.equals(user.getId())) {
                return "false";
            }
            return "true";
        } catch (Exception e) {
            // TODO: handle exception
            return "true";
        }
    }

    /**
     * 用户Email是否存在
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/userEmailExists")
    @ResponseBody
    public String userEmailExists(@PathParam(required = false) String email, @PathParam(required = false) Long id, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isBlank(email)) {
            return "true";
        }
        try {
            User user = userService.findByEmail(email);
            if (user == null) {
                return "false";
            }
            if (id != null && id.equals(user.getId())) {
                return "false";
            }
            return "true";
        } catch (Exception e) {
            // TODO: handle exception
            return "true";
        }
    }

    /**
     * 用户别名是否存在
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/userAliasExists")
    @ResponseBody
    public String userAliasExists(@PathParam(required = false) String alias, @PathParam(required = false) Long id, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isBlank(alias)) {
            return "true";
        }
        try {
            User user = userService.findByAlias(alias);
            if (user == null) {
                return "false";
            }
            if (id != null && id.equals(user.getId())) {
                return "false";
            }
            return "true";
        } catch (Exception e) {
            // TODO: handle exception
            return "true";
        }
    }

    /**
     * 判断分类名称是否存在
     *
     * @param name
     * @param id
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/catalogNameExists")
    @ResponseBody
    public String catalogNameExists(@PathParam(required = false) String name, @PathParam(required = false) Long id, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isBlank(name)) {
            return "true";
        }
        Catalog catalog = catalogService.findByName(name);
        if (catalog == null) {
            return "false";
        }
        if (id != null && id.equals(catalog.getId())) {
            return "false";
        }
        return "true";
    }

    /**
     * 检测角色名字是否存在
     *
     * @param name
     * @param id
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/roleNameExists")
    @ResponseBody
    public String roleNameExists(@PathParam(required = false) String name, @PathParam(required = false) Long id, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isBlank(name)) {
            return "true";
        }
        Role role = roleService.findByName(name);
        if (role == null) {
            return "false";
        }
        if (id != null && id.equals(role.getId())) {
            return "false";
        }
        return "true";
    }

    /**
     * 检测角色名字是否存在
     *
     * @param name
     * @param id
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/tagNameExists")
    @ResponseBody
    public String tagNameExists(@PathParam(required = false) String name, @PathParam(required = false) Long id, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isBlank(name)) {
            return "true";
        }
        Tag tag = tagService.findByName(name);
        if (tag == null) {
            return "false";
        }
        if (id != null && id.equals(tag.getId())) {
            return "false";
        }
        return "true";
    }
}
